{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Create csv data for Reuters-21578 dataset\n",
    "\n",
    "run the following cell to generate the csv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import json\n",
    "import codecs \n",
    "import os\n",
    "import unicodecsv\n",
    "from collections import OrderedDict\n",
    "import os\n",
    "\n",
    "#data_size = \"justTen\"\n",
    "data_size = \"full\"\n",
    "outfile = os.path.abspath(\"reuters-21578.csv\")\n",
    "\n",
    "row_count = 0\n",
    "ordered_fieldnames = OrderedDict([('id',None),('title',None)])\n",
    "with open(outfile,'wt') as f:\n",
    "    dw = unicodecsv.DictWriter(f, delimiter=',', fieldnames=ordered_fieldnames, encoding='utf-8')\n",
    "    dw.writeheader()\n",
    "    \n",
    "    for filename in os.listdir(\"reuters-21578-json/data/\"+data_size):\n",
    "        f = open(\"reuters-21578-json/data/\"+data_size+\"/\"+filename)\n",
    "        js = json.load(f)\n",
    "        for j in js:\n",
    "            if 'topics' in j and 'body' in j:\n",
    "                r = {}\n",
    "                r[\"id\"] = j['id']\n",
    "                r[\"title\"] = j['title']\n",
    "                dw.writerow(r)\n",
    "                row_count += 1\n",
    "\n",
    "print \"finished writing csv data\".format(**locals())\n",
    "print \"rows: {row_count}\".format(**locals())\n",
    "print \"file: {outfile}\".format(**locals())\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
